package com.cloudfun.campusshare.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;

/**
 * 城市三级联动
 *
 * @author gcg
 * @create 2018-06-22 17:06
 **/
public class CityUtil {

    private CityUtil() {}

    /**
     * 城市json
     */
    public static JSONArray CITY = null;

    /**
     * 城市json
     */
    public static JSONArray CITY_PROVINCE = null;

    /**
     * 获取城市josn文件
     * @return
     * @throws IOException
     */
    public static JSONArray getCity() throws IOException {
        if (!Objects.isNull(CITY)) {
            return CITY;
        }
        // 读取配置文件
        InputStream inputStream = CityUtil.class.getClassLoader().getResourceAsStream("/static/city.json");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            CITY = JSONArray.fromObject(sb.toString());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return CITY;
    }

    /**
     * 获取城市josn文件
     * @return
     * @throws IOException
     */
    public static JSONArray getCityProvince() throws IOException {
        if (!Objects.isNull(CITY_PROVINCE)) {
            return CITY_PROVINCE;
        }
        // 读取配置文件
        InputStream inputStream = CityUtil.class.getClassLoader().getResourceAsStream("/static/province.json");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            CITY_PROVINCE = JSONArray.fromObject(sb.toString());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return CITY_PROVINCE;
    }

    /**
     *
     * @param regionCode 地区编号代码
     * @return 转换为实际地址的字符串
     * @throws IOException
     */
    public static String getFullName(String regionCode) throws IOException {
        //省
        String province = regionCode.substring(0,2);
        //市
        String city = regionCode.substring(2,4);
        //县（区）
        String country = regionCode.substring(4,6);
        String fullName = "";
        JSONArray cityJsonArray = CityUtil.getCity();
        //TODO:优化
        //TODO:by Zh.XiQ
        //地区编码转变为对应的实际地址（暴力转）
        for (int i=0;i<cityJsonArray.size();++i){
            JSONObject jsonObject = cityJsonArray.getJSONObject(i);
            if(jsonObject.getString("value").equals(province)){
                fullName += jsonObject.getString("label") + "/";
                JSONArray children = jsonObject.getJSONArray("children");
                for (int j=0;j<children.size();++j){
                    JSONObject childrenJSONObject = children.getJSONObject(j);
                    if (childrenJSONObject.getString("value").equals(province+city)){
                        fullName += childrenJSONObject.getString("label") + "/";
                        JSONArray children1 = childrenJSONObject.getJSONArray("children");
                        for(int k=0;k<children1.size();++k){
                            JSONObject jsonObject1 = children1.getJSONObject(k);
                            if (jsonObject1.getString("value").equals(province+city+country)){
                                fullName += jsonObject1.getString("label");
                                break;
                            }
                        }
                        break;
                    }
                }
                break;
            }
        }
        return fullName;
    }

    /**
     * 前两级转换
     * @param regionCode
     * @return
     * @throws IOException
     */
    public static String getSecondName(String regionCode) throws IOException {
        //省
        String province = regionCode.substring(0,2);
        //市
        String city = regionCode.substring(2,4);
        String fullName = "";
        JSONArray cityJsonArray = CityUtil.getCity();
        //TODO:优化
        //地区编码转变为对应的实际地址（暴力转）
        for (int i=0;i<cityJsonArray.size();++i){
            JSONObject jsonObject = cityJsonArray.getJSONObject(i);
            if(jsonObject.getString("value").equals(province)){
                fullName += jsonObject.getString("label") + "/";
                JSONArray children = jsonObject.getJSONArray("children");
                for (int j=0;j<children.size();++j){
                    JSONObject childrenJSONObject = children.getJSONObject(j);
                    if (childrenJSONObject.getString("value").equals(province+city)){
                        fullName += childrenJSONObject.getString("label");
                        break;
                    }
                }
                break;
            }
        }
        return fullName;
    }
}
